package com.ef.dao.impl;

import java.util.List;

import com.ef.dao.UserDao;
import com.ef.domain.School;
import com.ef.domain.User;

public class UserDaoImpl extends DaoImpl<User> implements UserDao {

	@SuppressWarnings("unchecked")
	public User getUser(String loginName) {
		List<User> users = this.getHibernateTemplate()
				.findByNamedQueryAndNamedParam("getPassword", "loginName",
						loginName);
		if (users.size() > 0) {
			return users.get(0);
		} else {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public User findByCaller(String telCode) {
		List<User> users = this.getHibernateTemplate().find(
				"from User u where u.status='VALID' and telCode = '" + telCode
						+ "' ");
		if (users.size() > 0) {
			return users.get(0);
		} else {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<User> findByRoleName(String name) {
		return this.getHibernateTemplate().find(
				"from User u where u.status='VALID' and  u.role.name = '"
						+ name + "'");
	}

	@SuppressWarnings("unchecked")
	public List<User> findByRoleAndSchool(String roleName, School school) {
		return this.getHibernateTemplate().find(
				"from User u where u.status='VALID' and  u.role.name like '%"
						+ roleName + "' and u.school.id = '" + school.getId()
						+ "' ");
	}

	@SuppressWarnings("unchecked")
	public User findByWorkNo(String workNo) {
		List<User> users = this.getHibernateTemplate().find(
				"from User u where u.status='VALID' and  u.workcode = '"
						+ workNo + "'  ");
		if (users != null && users.size() > 0) {
			return users.get(0);
		}
		return null;
	}

}
